<?php
/**
 * Move candidate to another step of the workflow
 * 
 * 
 */

use Fox\Html\Input;
use Fox\Html\Button;
use Hive\Mission\Partner;
use Fox\Html\TextArea;
use Fox\Generator;
use Fox\Html\DropList;
use Fox\Html\Screen;
use Fox\Popup;
$xview = Xview::instance();
$currentStepId = $xview->val('currentstep');
$checked = $xview->val('checked');
$checked = str_replace('"',"'",$checked);
$mission = $xview->component("missions", "object");
$mid = $mission->getId();
$screen = new Screen('sMove','bmove',null,true,Generator::ajaxReload('contentCol'));
$screen->buttonBarClass('text-center');
$screen->buttons('bmove')
		->label(Msg(Hive::BUNDLE,Hive::MOVE))
		->color(Button::COLOR_WARNING);
		
$screen->block()
		->add(new DropList(array('workflow_step','status'),true,false,true))
			->options($mission->workflow()->steps(),'stepid',Generator::call('label'))
			->unsetOption($currentStepId);
$screen->block()
		->add(new TextArea('comments'));
$screen->block()->add(new Input('checked',true,$checked))
			->hidden();
if($screen->submitted()) {
	$fields = $screen->get();
	if(!$screen->errors()) {
		try {
			$checked = str_replace("'",'"',$checked);
			$partnerIds = json_decode($checked,true);
			if(is_array($partnerIds)) {
				$nbContact = count($partnerIds);
				$infos = Fox\Dao::infos(Partner::className());
				$tableName = $infos->tableName();
				$handle = $infos->handle();
				$handle->beginTransaction();
				foreach($partnerIds as $partnerid) {
					$handle->update($tableName,array('status' => $fields['status'],'previous_status' => $currentStepId,'comments' => $fields['comments']),
										array('missionid' => $mid,'role' => Partner::ROLE_CANDIDATE,'partnerid' => $partnerid));
				}
				$handle->commit();
			}
			return Msg(Hive::BUNDLE,Hive::MSG_MISSION_CONTACTS_ASSIGNED,$nbContact);
		} catch(Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		}
		
	}
	return Xview::instance()->messages(true,true);
}
$popup = new Popup('pMove',Msg(Hive::BUNDLE,Hive::MOVE),$screen);
return $popup->display();